//
//  LPTPhysicianListViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/3.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTPhysicianListViewController.h"
#import "LPTHoriScrollView.h"
#import "LPTPhysicianListTableViewCell.h"
#import "LPTpatientsDetailViewController.h"
#import "ChatViewController.h"

@interface LPTPhysicianListViewController ()<UITableViewDelegate,UITableViewDataSource,LPTPhysicianListTableViewCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)NSMutableArray *dataSource;
@property (assign, nonatomic)NSInteger index;
@property (strong, nonatomic)NSIndexPath *indexPath;

@end

@implementation LPTPhysicianListViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushToLPTpatientsDetailViewController"]) {
        
        NSDictionary *dic = self.dataSource[self.indexPath.row];
        LPTpatientsDetailViewController *vc = segue.destinationViewController;
        if ([dic[@"status"] integerValue] == 1) {
         
            //待诊断
            vc.type = kDaizhenduanType;
            vc.zhenduanid = dic[@"id"];
        }else{
            
            //诊断
            vc.type = kWenzhenType;
            vc.zhenduanid = dic[@"id"];
        }
    }
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.dataSource = [[NSMutableArray alloc] init];
    self.index = 0;
    [self initSubViews];
    
    [self getWenzhenListWithStatus:self.index];
}

-(void)initSubViews
{
    LPTHoriScrollView *horiView = [[LPTHoriScrollView alloc] initWithTitleArray:@[@"全部",@"待诊断",@"待评价",@"已完成"] frame:CGRectMake(0, kTopBarHeight, kScreenWidth, 46) selectBlock:^(NSInteger index) {
        
        self.index = index;
        [self getWenzhenListWithStatus:index];
        
    }];
    
    [self.view addSubview:horiView];
    self.tableView.rowHeight = 196;
    self.tableView.tableFooterView = [UIView new];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.view).mas_offset(kTopBarHeight + horiView.height);
    }];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getWenzhenListWithStatus:self.index];
        
    }];
    self.tableView.mj_header = header;
    
    [self.tableView reloadData];
}

-(void)getWenzhenListWithStatus:(NSInteger)status
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:@{@"page":@(1),@"size":@(1000)}];
    
    if (status > 0) {
        [params setValue:@(status) forKey:@"status"];
    }
    
    if (!self.dataSource.count) {
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/text_visit_list" withBody:params];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:data[@"visits"]];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.dataSource removeAllObjects];
        [self.tableView reloadData];
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithError:data];
        
    }];
}

#pragma mark - UITableViewDataSource & Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LPTPhysicianListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTPhysicianListTableViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    [cell refreshWithDic:self.dataSource[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - LPTPhysicianListTableViewCellDelegate
-(void)physicianListTableViewCell:(LPTPhysicianListTableViewCell *)cell didClickBtnWithIndex:(NSInteger)index
{
    self.indexPath = [self.tableView indexPathForCell:cell];
    
    NSDictionary *dict = self.dataSource[self.indexPath.row];
    
    if (index == 0) {
        
        //诊断
        [self performSegueWithIdentifier:@"pushToLPTpatientsDetailViewController" sender:nil];
        
    }else{
        NSNumber *idStr = [dict valueForKey:@"id"];
        //发消息
        ChatViewController *chatVc = [ChatViewController initstance];
        chatVc.visit_id = [idStr integerValue];
        chatVc.chatPersonName = [[dict valueForKey:@"user"] valueForKey:@"name"];
        chatVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:chatVc animated:YES];
        chatVc.hidesBottomBarWhenPushed = NO;
    }
}

@end
